import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import store from '../store/index.js'



function nextRouter(path,next){
  // 判断你的用户权限中是否包含当前路由地址，如果包含的话就让你进入，否则就让你进到首页
  // 取出你能去的权限地址
  let routerArr = store.state.user.menus_url

  if(routerArr.includes(path)){
    next()
  }else{
    next("/")
  }
}


export const twoRouter =[
  {
    path:"menu",
    meta:{
        title:"菜单管理"
    },
    beforeEnter: (to, from, next) => {
      nextRouter("/menu",next)
    },
    component:()=> import("../page/menu/menu.vue")
},{
    path:"role",
    meta:{
      title:"角色管理"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/role",next)
  },
    component:()=> import("../page/role/role.vue")
},{
    path:"manage",
    meta:{
      title:"管理员管理"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/manage",next)
  },
    component:()=> import("../page/manage/manage.vue")
},{
    path:"cate",
    meta:{
      title:"商品分类"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/cate",next)
  },
    component:()=> import("../page/cate/cate.vue")
},{
    path:"specs",
    meta:{
      title:"商品规格"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/specs",next)
  },
    component:()=> import("../page/specs/specs.vue")
},{
    path:"goods",
    meta:{
      title:"商品管理"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/goods",next)
  },
    component:()=> import("../page/goods/goods.vue")
},{
    path:"member",
    meta:{
      title:"会员管理"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/member",next)
  },
    component:()=> import("../page/member/member.vue")
},{
    path:"banner",
    meta:{
      title:"轮播图管理"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/banner",next)
  },
    component:()=> import("../page/banner/banner.vue")
},{
    path:"seckill",
    meta:{
      title:"秒杀活动"
  },
  beforeEnter: (to, from, next) => {
    nextRouter("/seckill",next)
  },
    component:()=> import("../page/seckill/seckill.vue")
}
]


const routes = [
  {
    path:"/login",
    component:()=>import("../page/login/login.vue")
  },
  {
    path:"/",
    component:()=>import("../page/index/index.vue"),
    children:[
      {
        path:"",
        component:()=> import("../page/home/home.vue")
      },
    ...twoRouter]
  }
]

const router = new VueRouter({
  routes
})

  //全局前置守卫，注册拦截并存入
router.beforeEach((to,from,next) => {
  console.log(to);
  console.log(from);

  if(to.path == "/login"){
    next()
    return
  }
  if(store.state.user.token){
    next()
    return
  }
    next("/login")
})

export default router
